<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/2/19
 * Time: 9:50
 */

namespace app\admin\controller;


use think\Db;
use think\facade\Request;
use app\admin\model\News as NewsModel;
use app\admin\model\NewsCate as NewsCateModel;
class News extends Common
{
    /**
     * 新闻列表
     * @return array|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function newsList()
    {
        if (Request::isAjax()) {
            $title = trim(input("post.title"))?trim(input("post.title")):"";
            $author = trim(input("post.author"))?trim(input("post.author")):"";
            $editor = trim(input("post.editor"))?trim(input("post.editor")):"";
            $cate_id = trim(input("post.cate_id"))?trim(input("post.cate_id")):"";

            // TODO:获取where条件
            $where[] = ["b.title","like",'%'.$title.'%'];
            $where[] = ["b.author","like",'%'.$author.'%'];
            $where[] = ["b.editor","like",'%'.$editor.'%'];
            $where[] = ["b.is_del","=",1];
            $where[] = ["a.is_del","=",1];
            if($cate_id != 0)
            {
                $where[] = ["b.cate_id","=",$cate_id];
            }

            //获取分页参数
            $pageOption = $this->getPageAndPageSize(input('page'),input('limit'));

            //获取列表
            $field = "b.*";
            $list = NewsModel::getPageListWithNewsCate($pageOption['page'],$pageOption['pageSize'],$where,['b.create_time'=>'desc'],$field);
            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }

        // 获取栏目列表
        $where = [];
        $where[] = ["is_del","=",1];
        $cate_list = NewsCateModel::getFieldValue('id,cate_name',$where,'true',['cate_id'=>"desc",'sort'=>"asc"]);
        $this->assign("cate_list",$cate_list);
        return view("news_list");
    }

    /**
     * 新闻添加
     * @return array|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function newsAdd()
    {
        if (Request::isAjax()) {
            $data = Request::except('file');
            $data['create_time'] = time();
            $data['update_time'] = $data['create_time'];

            //验证
            $where[] = ["title","=",$data['title']];
            $info = NewsModel::getFieldValue('id,title',$where);
            if(!empty($info['title']))
            {
                return $result = ["code"=>1,"msg"=>"已存在相同的新闻标题，请重新输入！","data"=>[]];
            }

            //添加
            $r = NewsModel::create($data);
            if($r !== false)
            {
                return $result = ["code"=>0,"msg"=>"插入成功","data"=>[]];
            }
            else{
                return $result = ["code"=>1,"msg"=>"插入失败","data"=>[]];
            }
        }
        // 获取栏目列表
        $where = [];
        $where[] = ["is_del","=",1];
        $cate_list = NewsCateModel::getFieldValue('id,cate_name',$where,'true',['cate_id'=>"desc",'sort'=>"asc"]);
        $this->assign("cate_list",$cate_list);
        return view("news_add");
    }

    /**
     * 新闻编辑
     * @return array|\think\response\View
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function newsEdit()
    {
        if (Request::isAjax()) {
            $data = Request::except('file');
            $data['update_time'] = time();
            $id = $data['id'];
            unset($data['id']);

            //验证
            $where[] = ["title","=",$data['title']];
            $where[] = ["id","<>",$id];
            $info = NewsModel::getFieldValue('id,title',$where);
            if(!empty($info['title']))
            {
                return $result = ["code"=>1,"msg"=>"已存在相同的新闻标题，请重新输入！","data"=>[]];
            }

            //添加
            $where = [];
            $where[] = ["id","=",$id];
            $r = NewsModel::updateOneData($where,$data);
            if($r !== false)
            {
                return $result = ["code"=>0,"msg"=>"更新成功","data"=>[]];
            }
            else{
                return $result = ["code"=>1,"msg"=>"更新失败","data"=>[]];
            }
        }
        $id = trim(input("id"));
        // 获取栏目列表
        $where = [];
        $where[] = ["is_del","=",1];
        $cate_list = NewsCateModel::getFieldValue('id,cate_name',$where,'true',['cate_id'=>"desc",'sort'=>"asc"]);
        $this->assign("cate_list",$cate_list);

        //获取单条新闻信息
        $where[] = ['id',"=",$id];
        $info = NewsModel::getFieldValue("*",$where);
        $content=$info['content'];
        if(get_magic_quotes_gpc()){
            $content=stripslashes($content);
        }
        $info['content']=htmlspecialchars_decode($content);
        $this->assign("info",$info);
        return view("news_edit");
    }

    /**
     * 新闻删除
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function newsDel()
    {
        if (Request::isAjax()) {
            $id = trim(input("id"));

            //更新is_del
            $r = NewsModel::softDel($id);
            if($r !== false)
            {
                return $result = ["code"=>0,"msg"=>"删除成功","data"=>[]];
            }
            else{
                return $result = ["code"=>1,"msg"=>"删除失败","data"=>[]];
            }
        }
    }
}